package cn.com.yusys.yusp.admin.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class SessionRequestInterceptor implements HandlerInterceptor {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


//    private RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
    /*
    在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("执行了SessionRequestInterceptor的preHandle方法");
//        RedisConnectionFactory connectionFactory = new JedisConnectionFactory();
//        RedisConnection conn = connectionFactory.getConnection();
        String authorization = request.getHeader("Authorization");
        if(authorization==null||"".equals(authorization)){
            return true;
        }
        String[] s = authorization.split(" ");
//        System.out.println(authorization);
//        System.out.println(s.length);
//        System.out.println(s[0]);
//        System.out.println(s[s.length-1]);
        Date now = new Date();
        long time30 = 30*60*1000;
        long time40 = 40*60*1000;
        stringRedisTemplate.expireAt("access:" + s[s.length-1],new Date(now.getTime()+time30));
        stringRedisTemplate.expireAt("access_to_refresh:" + s[s.length-1],new Date(now.getTime()+time40));
        stringRedisTemplate.expireAt("auth:" + s[s.length-1],new Date(now.getTime()+time30));
        stringRedisTemplate.expireAt("refresh:" + s[s.length-1],new Date(now.getTime()+time40));
        stringRedisTemplate.expireAt("refresh_auth:" + s[s.length-1],new Date(now.getTime()+time40));
        stringRedisTemplate.expireAt("refresh_to_access:" + s[s.length-1],new Date(now.getTime()+time40));
//        byte[] access = conn.get(this.serializationStrategy.serialize());
//        byte[] access_to_refresh = conn.get(this.serializationStrategy.serialize());
//        byte[] auth = conn.get(this.serializationStrategy.serialize();
//        byte[] refresh = conn.get(this.serializationStrategy.serialize());
//        byte[] refresh_auth = conn.get(this.serializationStrategy.serialize());
//        byte[] refresh_to_access = conn.get(this.serializationStrategy.serialize());
//        int second1800 = Long.valueOf(1800).intValue();
//        int second2400 = Long.valueOf(2400).intValue();
//        conn.expire(access, (long)second1800);
//        conn.expire(auth, (long)second1800);
//        conn.expire(access_to_refresh, (long)second2400);
//        if(refresh!=null){
//            conn.expire(refresh, (long)second2400);
//        }
//        if(refresh_auth!=null){
//            conn.expire(refresh_auth, (long)second2400);
//        }
//        if(refresh_to_access!=null){
//            conn.expire(refresh_to_access, (long)second2400);
//        }
//        System.out.println(s[1]);
        return true;//如果为false，被请求时，拦截器执行到此处将不会继续操作。
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
//        System.out.println("执行了SessionRequestInterceptor的postHandle方法");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
//        System.out.println("执行了SessionRequestInterceptor的afterCompletion方法");
    }

}
